//添加新规则
	function addNewRule(){
		$("#newRule").show();
	}
	//取消编辑新规则
	function quitAddNewRule(){
		$("#newRule").hide();
	}
	//添加关键词
	function addKeyword(keyTable){
		//keyTable = 'keyword_0';
		var row = 0;
		$("#"+keyTable+" tr[id]").each(function(i){
			var trid = this.id;
			trid = trid.substr(trid.lastIndexOf("_")+1);
			//alert(trid);
			if(parseInt(trid) >= row){
				row = parseInt(trid) + 1;
			}
		});
		var html = "";
		html += '<tr id="'+keyTable+'_'+row+'" style="background-color: #FFF;">';
		html += '<td style="width:10%">';
		html += '<input type="text" name="keywords['+row+'].keyWord"/>'; 	
		html += '</td>';
		html += '<td style="text-align: right;">';
		html += '<input type="radio" name="keywords['+row+'].matchType" value="0" checked="checked"/>不全匹配    ';
		html += '<input type="radio" name="keywords['+row+'].matchType" value="1"/>全匹配    ';
		html += '<a href="javascript:deleteKeyword(\''+keyTable+'_'+row+'\')">删除</a>';
		html += '</td>';
		html += '</tr>';
		if(row > 0){
			$("#"+keyTable+'_'+(row-1)).after(html);
		}else{
			$("#"+keyTable).append(html);
		}
	}
	//删除关键词
	function deleteKeyword(keywordId){
		$("#"+keywordId).remove();
	}
	function checkForm(ruleFormId){
		var formIndex = ruleFormId.substr(ruleFormId.lastIndexOf("_")+1);
		var ruleName = $("#"+ruleFormId+" input[name='ruleName']").get(0).value;
		var replyType ;
		 $("#"+ruleFormId+" input[name='material.materialType']").each(function(i){
			 if(this.checked == true){
				 replyType = this.value;
			 }
		 });
		 //alert(replyType);
		if(ruleName == "" || ruleName == null){
			//alert("规则名不能为空");
			showToast(false,"规则名不能为空");
			return false;
		}
		var keyTableId = "keyTable_"+ruleFormId.substr(ruleFormId.lastIndexOf("_")+1);
		var keywords = $("#"+keyTableId+" input[type='text']");
		if(keywords.length > 0){
			var isEmpty = false;
			keywords.each(function(i){
				var keyword = this.value;
				if(keyword == "" || keyword == null){
					isEmpty = true;
				}
			});
			if(isEmpty){
				//alert("关键词不能为空");
				showToast(false,"关键词不能为空");
				return false;
			}
		}else{
			//alert("关键词不能为空");
			showToast(false,"关键词不能为空");
			return false;
		}
		if(replyType == "text"){
			var content = $("#"+ruleFormId+" textarea").val();
			if(content == "" || content == null){
				//alert("回复内容不能为空");
				showToast(false,"回复内容不能为空");
				return false;
			}
		}else{
			var materialId = $("#reply_material_"+formIndex).val();
			if(materialId == "" || materialId == null){
				showToast(false,"请选择一条回复素材");
				return false;
			}
		}
		return true;
	}
	//验证自动回复规则表单 正确提交表单
	function saveRules(ruleFormId,type){
		var flag = checkForm(ruleFormId);
		var url = "";
		if(type == "add"){
			url = rootPath+'/autoReply/add.mge';
		}else if(type == "update"){
			url = rootPath+'/autoReply/update.mge';
		}
		if(flag){
			$.ajax({
				contentType : "application/x-www-form-urlencoded; charset=UTF-8", // ajax编码转换
				traditional : true, // 是否深度序列化参数对象
				cache : false, // 缓存设置
				type : "POST", // 如果不指定method，则默认为Post
				url : url, // 请求的URL
				data : $("#"+ruleFormId).serialize(), // 请求时发送的Http参数
				dataType : "json",// 返回的数据类型，默认为json
				success : function(data){
					if(data.state == '200'){
						location.reload();
					}else{
						//alert(data.msg);
						showToast(false,data.msg);
					}
				}
			});
		}
	}
	//是否编辑已有规则
	function ruleEdit(ruleTable){
		var index = ruleTable.substr(ruleTable.lastIndexOf("_")+1);
		if( $("#rule_table_view_"+index).is(":hidden") ){
			$("#rule_table_view_"+index).show();
			$("#rule_table_edit_"+index).hide();
		}else{
			$("#rule_table_view_"+index).hide();
			$("#rule_table_edit_"+index).show();
		}
	}
	function deleteRule(ruleId){
		var url = rootPath+'/autoReply/delete.mge?id='+ruleId;
		$.ajax({
			contentType : "application/x-www-form-urlencoded; charset=UTF-8", // ajax编码转换
			traditional : true, // 是否深度序列化参数对象
			cache : false, // 缓存设置
			type : "delete", // 如果不指定method，则默认为Post
			url : url, // 请求的URL
			data : "id="+ruleId, // 请求时发送的Http参数
			dataType : "json",// 返回的数据类型，默认为json
			success : function(data){
				if(data.state == '200'){
					location.reload();
				}else{
					//alert(data.msg);
					showToast(false,data.msg);
				}
			}
		});
	}
	//图文div居中
	$(window).scroll(function(){
		var theScrollTop = document.body.scrollTop||document.documentElement.scrollTop;
		var theScrollLeft = document.body.scrollLeft||document.documentElement.scrollLeft;
		var boxWidth = document.getElementById("dialog").style.width;
		boxWidth = boxWidth.substr(0,boxWidth.lastIndexOf("p"));
		var leftPosition = (theScrollLeft+(document.body.clientWidth-boxWidth)/2)+"px";
		//alert(boxWidth);
		  $("#dialog").css({
	          left:  leftPosition,
	          top:  theScrollTop+10+"px",
	          position: 'absolute'
	      });
	});
	//初始化图片宽高
	$(document).ready(function(){
		 $(".item_img").each(function(i){
			var imgWidth = $(this).width();
			var imgHeight = $(this).height();
			if(imgWidth > imgHeight){
				$(this).attr("width","100%");
			}else{
				$(this).attr("height","100%");
			}
		});
	});
	//选择恢复类型
	function selectReplyType(type,index){
		//alert("index="+index);
		var replyContentId = "replyContent_"+index;
		if(type == "text"){
			var contentHtml = '<textarea rows="3" cols="50" name="material.content"></textarea>';
			$("#"+replyContentId).html("");
			$("#"+replyContentId).html(contentHtml);
		}else if(type == "news"){
			$("#"+replyContentId).html("");
			$("#selected_reply_index").val(index);
			$("#mask").show();
			$("#dialog").show();
		}
	}
	
	function selectMaterial(type){
		//alert(type);
		var materialId,material_index;
		var reply_index = $("#selected_reply_index").val();
		//alert("index="+index);
		if(type == "news"){
			var flag = false;
			$("input[name='news_id']").each(function(i){
				//alert("value="+this.value+"checked="+this.checked);
				if(this.checked == true){
					materialId = this.value;
					material_index = this.id;
					flag = true;
				}
			});
			//alert("material_index="+material_index+";flag="+flag);
			//alert("materialId="+materialId);
			if(flag){
				$("#reply_material_"+reply_index).val(materialId);
				var selectTr = $("#news_tr_"+material_index).clone();
				$("#news_tr_"+material_index).remove();
				$("#replyContent_"+reply_index).html("");
				var materialClone = $("#material_news_"+material_index).clone();
				materialClone.attr("id","material_clone_"+material_index);
				materialClone.appendTo("#replyContent_"+reply_index);
				selectTr.appendTo("#material_news_"+material_index); 
				$("#dialog").hide();
				$("#mask").hide();
			}else{
				//alert("请选择一个图文！");
				showToast(false,"请选择一个图文！");
			}
		}
	}
	function closeMaterial(type){
		if(type == "news"){
			$("#dialog").hide();
			$("#mask").hide();
		}
	}